import {SimpleSchema} from 'meteor/aldeed:simple-schema';
import {Meteor} from 'meteor/meteor';
class GatewaysCollection extends Mongo.Collection {
    update(selector, modifier) {
        const result = super.update(selector, modifier);
        return result;
    }
    insert(gateway, callback) {
        const ourGateways = gateway;
        const result = super.insert(ourGateways, callback);
        return result;
    }
    remove(selector, callback) {
        const result = super.remove(selector, callback);
        return result;
    }
}

export const Gateways = new GatewaysCollection('gateways');
const device = new SimpleSchema({
    d_id: {
        type: String,
        label: '设备Id',
        optional: true
    },
    d_name: {
        type: String,
        label: '设备名称',
        optional: true
    },
    gatewayId:{
        label:'网关id',
        type:String
    },
    isSensor:{
        label:'是否是传感器',
        type: Boolean
    },
    sensorType:{
        label:'传感器类型',
        type:String,
        optional: true
    },
    pin: {
        label: 'PIN码',
        type: String,
        optional: true
    },
    productNumber:{
        label:'设备型号',
        type:String
    },
    devType: {
        label: '设备类型',
        type: String
    },
    version:{
        label:'锁版本',
        type:String
    },
    roomId: {
        label: '所属房间ID',
        type: String,
        optional: true
    },
    roomName: {
        label: '所属房间名称',
        type: String,
        optional: true
    },
    c_date: {
        type: Date,
        label: '添加时间'
    },
    pwd: {
        type: String,
        label: '锁密码',
        optional: true
    },
    status: {
        type: String,
        label: '设备的状态',
        optional: false
    },
    isDef: {
        label: '是否为默认锁',
        type: Boolean,
        optional: true
    },
    ailse:{
        label: '通道数',
        type: Number
    },
    currentData:{
        label:'设备当前值',
        type:String,
        optional: true
    },
});
Gateways.deny({
    insert() { return true; },
    update() { return true; },
    remove() { return true; },
});

Gateways.schema = new SimpleSchema({
    name: {
        type: String,
        optional: true,
        label: '网关名称'
    },
    devId: {
        type: String,
        label: '网关编号'
    },
    devKey: {
        type: String,
        label: '网关键值'
    },
    devName: {
        type: String,
        label: '网关名'
    },
    /*password: {
        type: String,
        label: '网关密码',
        optional: true
    },*/
    devices: {
        label: '连接设备',
        type: [device],
        optional: true
    },
    createdId: {
        label: '创建人ID',
        type: String,
        optional: true
    },
    createdName: {
        label: '创建人名称',
        type: String,
    },
    familyId: {
        type: String,
        label: '网关所属家庭'
    },
    mqtt_msg: {
        label: '发送消息',
        type: String,
    },
    // user_role: {
    //     label: '角色',
    //     type: [String],
    // },
    // group_id: {
    //     label: '所属团体Id',
    //     type: String,
    //     optional: true
    // },
    // group_name: {
    //     label: '所属团体',
    //     type: String,
    //     optional: true
    // },
    // room_id: {
    //     type: String,
    //     label: '所属房间Id',
    //     optional: true
    // },
    // room_name: {
    //     type: String,
    //     label: '所属房间',
    //     optional: true
    // },
    isDefault: {
        label: '是否为默认网关',
        type: Boolean,
        optional: true
    },
    updatedAt: {
        type: Date,
        optional: true,
        autoValue() {
            if (this.isUpdate) {
                return new Date();
            }
        }
    },
    createdAt: {
        type: Date,
        optional: true,
        label: '创建时间',
        autoValue() {
            if (this.isInsert) {
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }
            return this.unset();  // Prevent user from supplying their own value
        }
    }
});
Gateways.attachSchema(Gateways.schema);